
package me.liuhui.mall.manager.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import me.liuhui.mall.common.base.vo.ResultVO;
import me.liuhui.mall.manager.service.OrderService;
import me.liuhui.mall.manager.service.convertor.OrderConverter;
import me.liuhui.mall.manager.service.dto.order.CreateOrderDTO;
import me.liuhui.mall.manager.service.dto.order.ListOrderDTO;
import me.liuhui.mall.manager.service.dto.order.ModifyOrderDTO;
import me.liuhui.mall.manager.service.vo.order.ListOrderVO;
import me.liuhui.mall.repository.mapper.OrderMapper;
import me.liuhui.mall.repository.model.Order;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.Objects;
import java.util.Set;

/**
 * Created on 2020/10/14 20:12
 * <p>
 * Description: [TODO]
 * <p>
 *
 * @author [清远]
 */
@Service
public class OrderServiceImpl implements OrderService {
    @Resource
    private OrderConverter orderConverter;
    @Resource
    private OrderMapper orderMapper;

	@Override
	public ResultVO<ListOrderVO> list(ListOrderDTO dto) {
		if (StrUtil.isBlank(dto.getOrderBy())) {
			dto.setOrderBy("id desc");
		}
		LambdaQueryWrapper<Order> wrapper = Wrappers.lambdaQuery();
		wrapper.gt(Objects.nonNull(dto.getMinCreateTime()), Order::getCreateTime, dto.getMinCreateTime())
				.lt(Objects.nonNull(dto.getMaxCreateTime()), Order::getCreateTime, dto.getMaxCreateTime())
				.gt(Objects.nonNull(dto.getMinPayTime()), Order::getPayTime, dto.getMinPayTime())
				.lt(Objects.nonNull(dto.getMaxPayTime()), Order::getPayTime, dto.getMaxPayTime())
				.eq(StrUtil.isNotBlank(dto.getNo()), Order::getNo, dto.getNo())
				.eq(StrUtil.isNotBlank(dto.getConsigneeAddress()), Order::getConsigneeAddress, dto.getConsigneeAddress())
				.eq(StrUtil.isNotBlank(dto.getConsigneeCellphone()), Order::getConsigneeCellphone, dto.getConsigneeCellphone())
				.eq(StrUtil.isNotBlank(dto.getConsigneeName()), Order::getConsigneeName, dto.getConsigneeName())
				.eq(StrUtil.isNotBlank(dto.getLogisticNo()), Order::getLogisticNo, dto.getLogisticNo())
				.eq(StrUtil.isNotBlank(dto.getConsigneeTel()), Order::getConsigneeTel, dto.getConsigneeTel())
				.eq(Objects.nonNull(dto.getConsumerUserId()), Order::getConsumerUserId, dto.getConsumerUserId())
				.eq(Objects.nonNull(dto.getId()), Order::getId, dto.getId())
				.eq(Objects.nonNull(dto.getTotalAmount()), Order::getTotalAmount, dto.getTotalAmount());
		Page<Order> page = new Page<>(dto.getPage(), dto.getLimit());

        Page<Order> orders = orderMapper.selectPage(page, wrapper);
        ListOrderVO vo = new ListOrderVO();
		vo.setTotal(orders.getTotal());
		vo.setList(orderConverter.toVo(orders.getRecords()));
		return ResultVO.buildSuccessResult(vo);
	}

	@Override
	public ResultVO<Boolean> create(CreateOrderDTO dto) {
		Order entity = orderConverter.createDtoToEntity(dto);
        entity.setCreateTime(new Date());
        orderMapper.insert(entity);
		return ResultVO.buildSuccessResult();
	}

	@Override
	public ResultVO<Boolean> modify(ModifyOrderDTO dto) {
        Order order = orderMapper.selectById(dto.getId());
		if (order == null) {
			return ResultVO.buildFailResult("角色不存在");
		}


		Order entity = orderConverter.modifyDtoToEntity(dto);
        orderMapper.updateById(entity);
		return ResultVO.buildSuccessResult();
	}

	@Override
	public ResultVO<Boolean> delete(Set<Long> ids) {
		for (Long id : ids) {
            orderMapper.deleteById(id);
		}
		return ResultVO.buildSuccessResult();
	}

}
